//
// Created by DengLibin on 2018/12/12 0012.
//

/**
 * 字符串
 */

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
using namespace std;
//1、创建字符串
void testCreateString()
{
    string s1 = "hello world";
    string s2 = "hello China";
    string s3 = s2;
    string s4(10,'a');//10个a
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
}
void testBianLiString()
{
    string s1 = "abcdefg";
    //1、数组方式(如果越界不会往外抛异常,程序直接停止)
    for (int i = 0; i < s1.length(); i++)
    {
        cout << s1[i] << "\t";
    }
    cout << endl;
    //2、迭代器(一个指针，指向一个具体的元素)
    for (string::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    //3、函数(如果越界会向外抛异常)
    try
    {
        for (int i = 0; i < s1.length(); i++)
        {
            cout << s1.at(i) << " ";
        }
    }
    catch (...)
    {
        cout << "异常发生了" << endl;
    }
}
//字符串复制到字符数组
int copyStringToBuf(const string *s,char buf[], int start, int len)//字符串指针,字符数组指针,开始字符,复制的个数
{
    int strLen = s->length();
    if (start >= strLen)
    {
        cout << "开始字符不能大于字符穿的长度" << endl;
        return -1;
    }
    for (int i = start,j=0; i < (start+len); i++,j++)
    {
        buf[j] = (*s)[i];
    }
    return 0;
}
//3、字符指针和string的转换
void pointerString()
{
    string s1 = "hello world";
    //1、获取字符串的指针
    printf("s1:%s\n", s1.c_str());
    char buf[5] = { 0 };
    //2、将字符串拷贝到字符数组中
    copyStringToBuf(&s1,buf,4,4);
    buf[4] = '\0';//c语言中的字符串以'\0'结尾
    printf("%s\n", buf);
    cout << buf << endl;
}
//4、字符串的连接
void connectString()
{
    string s1 = "hello";
    string s2 = "world";
    string s3 = s1 + s2;
    string s4 = s1.append(s2);
    cout << s3 << endl; //helloworld
    cout << s1 << endl; //helloworld
    cout << s4 << endl; //helloworld

    ostringstream oss;
    oss << s1 << s2;
    string s5 = oss.str();
    cout << s5 << endl;//helloworldworld
}
//5、字符串的比较
void compareString()
{
    string s1 = "hello";
    string s2 = "world";
    int result1 = s2.compare(s1);//1
    int result2 = s2.compare(s2);//0
    int result3 = s1.compare(s2);//-1
    cout << result1 << endl;
    cout << result2 << endl;
    cout << result3 << endl;
}
//6、子串
void subString()
{
    string s1 = "mynameisPeter";
    string s2 = s1.substr(1, 6); //从1开始取6个字符
    cout << s2 << endl;//ynamei
}
//7、查找和替换
void findReplaceString()
{
    string s1 = "Hello,how are you,nice to meet you!";
    //1、查找
    int index = s1.find("are", 0);//从0开始往后找
    cout << index << endl;

    //2、查找某个字符串出现的次数以及每次出现的位置
    int offset = s1.find("you", 0);//第一次从0开始找
    int num = 0;
    while (offset != string::npos)//没有找完
    {
        cout << "位置:" << offset << endl;
        num++;
        offset = s1.find("you", offset + 1);//继续找，从当前位置的后一个位置开始往后找,如果找到最后一个字符串还是没找到，跳出循环
    }
    cout << "次数:" << num << endl;

    //3、替换(先查找后替换)将 you 换成 YOU
    int pos = s1.find("you", 0);
    while (pos != string::npos)
    {
        s1 = s1.replace(pos, 3, "YOU");
        pos = s1.find("you", pos + 1);
    }
    cout << "替换结果:" << s1 << endl;

}
//8、截断和插入
void deleteInsertString()
{
    string s1 = "hello world hello how are you nice to meet you";
    //删除 you
    for (int index = s1.find("you", 0); index != string::npos; index = s1.find("you", index))
    {
        s1 = s1.erase(index, 3);//删除you
        s1 = s1.insert(index, "me");//插入me(在index的前面插入)
    }
    cout << "删除后的字符串:" << s1 << endl;

    //在头部插入
    s1 = s1.insert(0, "头部");
    //在尾部插入
    s1 = s1.insert(s1.length(), "尾部");
    cout << s1 << endl;
    //删除整个字符串
    s1.erase(s1.begin(), s1.end());
    cout << s1 << s1.length() << endl;

}
//9、转换 (头文件:string, cctype,algorithm)
void translateString()
{
    string s1 = "AAABBccc";
    //转大写
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    cout << s1 << endl;
    //转小写
    transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
    cout << s1 << endl;
}
int main21(int argc, char* argv[])
{
    //testCreateString();
    //testBianLiString();
    //pointerString();
    //pointerString();
    //connectString();
    //subString();
    //findReplaceString();
    //deleteInsertString();
    translateString();
    return 0;
}